package routers

import (
	"comment/controllers"
	"comment/filters"
	"comment/util"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/context"
)

func init() {
	ns :=
		beego.NewNamespace("/v1")

	var apiFilter = func(ctx *context.Context) {
		code := 1
		var message string
		result, err := util.Get(ctx.Input.IP())
		if err != nil {
			code = 0
			message = "Limit Error(Get Redis Info Error 1)"
			beego.Error(err)
		} else {
			if result == nil {
				util.Set(ctx.Input.IP(), 1)
				time, err := beego.AppConfig.Int("limit_time")
				if err != nil {
					code = 0
					message = "Read Config (limit_time) Error"
					beego.Error(err)
				}
				util.Do("EXPIRE", ctx.Input.IP(), time)
			} else {
				count, err := util.GetInt(ctx.Input.IP())
				if err != nil {
					code = 0
					message = "Limit Error(Get Redis Info Error 2)"
					beego.Error(err)
				}
				number, err := beego.AppConfig.Int("limit_number")
				if err != nil {
					code = 0
					message = "Read Config (limit_number) Error"
					beego.Error(err)
				}
				if int(count) >= int(number) {
					code = 0
					message = "请求频率超过上限"
				} else {
					util.Do("INCRBY", ctx.Input.IP(), 1)
				}

			}
		}
		if code == 0 {
			ctx.WriteString(message)
		}
	}

	api :=
		beego.NewNamespace("/api",
			//校验
			beego.NSBefore(apiFilter),
			beego.NSRouter("/report/save", &controllers.ReportController{}, "*:Save"),
			beego.NSRouter("/report/find", &controllers.ReportController{}, "*:FindReport"),
		)
	beego.AddNamespace(api)
	beego.InsertFilter("/*", beego.BeforeRouter, filters.HasPermission)
	beego.Router("/login", &controllers.LoginController{})
	beego.Router("/", &controllers.IndexController{}, "GET:Main")
	beego.Router("/logout", &controllers.LoginController{}, "GET:Logout")
	beego.Router("/role/list", &controllers.RoleController{}, "*:List")
	beego.Router("/parametset/set/:id([0-9]+)", &controllers.ParametsetController{}, "*:Put")
	beego.Router("/role/perm/:id([0-9]+)", &controllers.RoleController{}, "GET:PermEdit")
	beego.Router("/role/perm/:id([0-9]+)", &controllers.RoleController{}, "Post:PermUpdate")

	beego.Router("/role/edit/:id([0-9]+)", &controllers.RoleController{}, "GET:Edit")
	beego.Router("/role/edit/:id([0-9]+)", &controllers.RoleController{}, "Post:Update")

	beego.Router("/role/add", &controllers.RoleController{}, "GET:Add")
	beego.Router("/role/add", &controllers.RoleController{}, "Post:Save")
	beego.Router("/role/delete/:id([0-9]+)", &controllers.RoleController{}, "GET:Delete")

	beego.Router("/report/list/:status([0-2])", &controllers.ReportController{}, "*:List")

	beego.Router("/report/detail/:id([0-9]+|PLJB[0-9]+)", &controllers.ReportController{}, "GET:Details")
	beego.Router("/report/audit/:id([0-9]+)", &controllers.ReportController{}, "POST:Audit")

	//评论管理
	beego.Router("comment/list/:state([0-2])", &controllers.CommentController{}, "*:CommentList")
	beego.Router("comment/detail/:state/:id", &controllers.CommentController{}, "get:CommentDeatil")
	beego.Router("comment/audit", &controllers.CommentController{}, "post:CommentAudit")
	beego.Router("comment/hot", &controllers.CommentController{}, "post:CommentHot")
	/**	敏感词*/
	beego.Router("/badword/index", &controllers.BadwordController{}, "Get:Index")
	beego.Router("/badword/delete/:id([0-9]+)", &controllers.BadwordController{}, "Get:Delete")
	beego.Router("/badword/edit/:id([0-9]+)", &controllers.BadwordController{}, "Post:Update")
	beego.Router("/badword/status/:id([0-9]+)", &controllers.BadwordController{}, "Get:ModifyStatus")
	beego.Router("/badword/import", &controllers.BadwordController{}, "Get:Import")
	beego.Router("/badword/add", &controllers.BadwordController{}, "Post:Add")
	beego.Router("/badword/batch-delete", &controllers.BadwordController{}, "Post:BatchDelete")
	beego.Router("/badword/batch-start", &controllers.BadwordController{}, "Post:BatchStart")
	beego.Router("/badword/batch-stop", &controllers.BadwordController{}, "Post:BatchStop")
	beego.Router("/badword/duplicate", &controllers.BadwordController{}, "*:Duplicate")
	//用户管理
	beego.Router("/user/list", &controllers.UserController{}, "*:List")
	beego.Router("/user/edit/:id([0-9]+)", &controllers.UserController{}, "GET:Edit")
	beego.Router("/user/edit/:id([0-9]+)", &controllers.UserController{}, "Post:Update")
	beego.Router("/user/add", &controllers.UserController{}, "GET:Add")
	beego.Router("/user/add", &controllers.UserController{}, "Post:Save")
	beego.Router("/user/delete/:id([0-9]+)", &controllers.UserController{}, "GET:Delete")
	beego.Router("/user/password/:id([0-9]+)", &controllers.UserController{}, "GET:Password")
	beego.Router("/user/password/:id([0-9]+)", &controllers.UserController{}, "Post:UpdatePassword")
	beego.AddNamespace(ns)
}
